<template>
	<view>
		<u-upload :fileList="fileList" @afterRead="afterRead" @delete="deletePic" :name="name" multiple
			:maxCount='maxCount'></u-upload>
	</view>
</template>

<script>
	import {
		globalData
	} from '@/common/globaldata.js';
	export default {
		data() {
			return {
				s: ''
			}
		},
		props: {
			maxCount: {
				type: Number,
				default: 1
			},
			fileList: {
				type: Array,
				default: []
			},
			name:{
				type:String,
				default:'one'
			}
		},
		methods: {
			// 删除图片
			deletePic(event) {
				// [`fileList${event.name}`].splice(event.index, 1)
				this.$emit('deletePic',event)
			},
			// 新增图片
			async afterRead(event) {
				uni.showLoading({
					title:'上传中'
				})
				console.log([`fileList${event.name}`],'sdfgf')
				this.s = [`fileList${event.name}`]
				// 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
				let lists = [].concat(event.file)
				let fileListLen = this.s.length
				lists.map((item) => {
					this.s.push({
						...item,
						status: 'uploading',
						message: '上传中'
					})
				})
				for (let i = 0; i < lists.length; i++) {
					const result = await this.uploadFilePromise(lists[i].url)
					console.log(result)
					let item = this.s[fileListLen]
					this.s.splice(fileListLen, 1, Object.assign(item, {
						status: 'success',
						message: '',
						url: result
					}))
					fileListLen++
				}
				uni.hideLoading()
				this.$emit('afterRead', this.s)
			},
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					let a = uni.uploadFile({
						url: globalData.uploadUrl,
						filePath: url,
						name: 'file',
						formData: {
		
						},
						// dataType: 'json',
						header: {

							'accept': 'application/json',
							'Authorization': uni.getStorageSync("token")
						},
						success: (res) => {
							// console.log(JSON.parse(res.data))
							let data = JSON.parse(res.data)
							if(data.code == '00000'){
								setTimeout(() => {
									resolve(data.data.url)
								}, 1000)
							}else{
								uni.showToast({
									title:data.msg,
									icon:'none'
								})
							}
							
						},
						fail() {
							uni.hideLoading()
						}
					});
				})
			},

		}
	}
</script>

<style>

</style>
